package com.hliushi.movie.web;

import com.hliushi.movie.entity.MovieDirector;
import com.hliushi.movie.service.MovieDetailService;
import com.hliushi.movie.service.MovieDirectorService;
import com.itheima.pinda.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 导演管理模块
 *
 * @author llhuang10
 * @date 2021/10/13 20:47
 */
@Slf4j
@Api(tags = "导演管理模块")
@Validated
@RestController
@RequestMapping("/api/director")
public class MovieDirectorController {

    @Resource
    private MovieDirectorService movieDirectorService;

    @Resource
    private MovieDetailService movieDetailService;

    /**
     * 添加电影导演
     *
     * @param movieDirector 电影导演
     * @return R
     */
    @PostMapping
    @ApiOperation("添加电影导演")
    public R insertDirector(@Valid MovieDirector movieDirector) {
        boolean res = movieDirectorService.insertDirector(movieDirector);
        return res ? R.success() : R.fail("添加电影导演失败, 请联系管理员");
    }

    /**
     * 修改电影导演
     *
     * @param movieDirector 电影导演
     * @return R
     */
    @PutMapping
    @ApiOperation("修改电影导演")
    public R updateDirector(@Valid MovieDirector movieDirector) {
        boolean res = movieDirectorService.updateDirector(movieDirector);
        return res ? R.success() : R.fail("修改电影导演失败, 请联系管理员");
    }

    /**
     * 根据Id查询导演信息
     *
     * @param id 导演id
     * @return 导演信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据Id查询导演信息")
    public R<MovieDirector> getById(@PathVariable("id") String id) {
        MovieDirector movieDirector = movieDirectorService.getById(id);
        return movieDirector != null ? R.success(movieDirector) : R.fail("查询失败");
    }

    /**
     * 返回一个电影的导演
     *
     * @param movieId 电影id
     * @return 导演信息
     */
    @GetMapping("/get")
    @ApiOperation("返回一个电影的导演")
    public R<MovieDirector> getByMovieId(@RequestParam("movieId") int movieId) {
        MovieDirector movieDirector = movieDetailService.getByMovieId(movieId);
        return movieDirector != null ? R.success(movieDirector) : R.fail("查询失败");
    }
}
